<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>执行任务列表 - SmartWebHook</title>
    {{template "widgets/headfile.html"}}
    <link rel="stylesheet" href="/static/codemirror/theme/ambiance.css">
    <script type="text/javascript" src="/static/vuejs/vue.min.js"></script>
    {{if .dev}}
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="/static/font-awesome/css/font-awesome.css">
    <link rel="stylesheet" href="/static/codemirror/lib/codemirror.css">
    <link rel="stylesheet" href="/static/css/global.css">
    <script src="/static/js/jquery-1.12.4.min.js" type="text/javascript"></script>
    <script src="/static/js/jquery.form.js" type="text/javascript"></script>
    {{end}}
</head>
<body>
{{template "widgets/head.html" .}}
<div class="container">
    <div class="bs-breadcrumb-container">
        <ol class="breadcrumb">
            <li> <i class="fa fa-home" aria-hidden="true"></i></li>
            <li><a href="{{urlfor "HomeController.Index"}}">WebHook</a> </li>
            <li><a href="{{urlfor "RelationController.Index" ":id" .Model.WebHookId}}">Servers</a> </li>
            <li>Scheduler List</li>
        </ol>

    </div>
    <div class="bs-body-container">
        <div class="title">
            <h3>Scheduler List</h3>
            <hr>
        </div>
        <div class="body">
            <table class="table table-condensed">
                <thead>
                <tr>
                    <th width="5%">#</th>
                    <th width="15%">Repo</th>
                    <th width="15%">Branch Name</th>
                    <th width="15%">Server Name</th>
                    <th width="15%">Add Time</th>
                    <th width="10%">Status</th>
                    <th width="15%">Console</th>
                </tr>
                </thead>
                <tbody>
                {{if .lists}}
                {{range $index,$item := .lists}}
                <tr>
                    <td>{{$item.SchedulerId}}</td>
                    <td>{{$.Model.RepositoryName}}</td>
                    <td>{{$.Model.BranchName}}</td>
                    <td>{{$.Model.ServerName}}</td>
                    <td>{{date $item.CreateTime "Y-m-d H:i:s"}}</td>
                    <td>
                        {{if eq $item.Status "wait"}}
                        <span class="label label-default">等待执行</span>
                        {{else if eq $item.Status "executing"}}
                        <span class="label label-primary">正在执行</span>
                        {{else if eq $item.Status "suspend"}}
                        <span class="label label-warning">执行取消</span>
                        {{else if eq $item.Status "failure"}}
                        <span class="label label-danger">执行失败</span>
                        {{else if eq $item.Status "success"}}
                        <span class="label label-success">执行成功</span>
                        {{else}}
                        <span class="label label-default">未知状态</span>
                        {{end}}
                    </td>
                    <td>
                        {{if eq $item.Status "wait"}}
                        <button data-id="{{$item.SchedulerId}}" class="btn btn-default btn-sm btn-cancel-schedule" data-toggle="tooltip" title="取消任务"><i class="fa fa-power-off" aria-hidden="true"></i></button>
                        {{end}}
                        {{if eq $item.Status "suspend" "failure"}}
                        <button data-id="{{$item.SchedulerId}}" class="btn btn-default btn-sm btn-resume-schedule" data-toggle="tooltip" title="重新执行"><i class="fa fa-repeat" aria-hidden="true"></i></button>
                       {{end}}
                        <button data-id="{{$item.SchedulerId}}" class="btn btn-default btn-sm btn-schedule-console" data-toggle="tooltip" title="执行日志"><i class="fa fa-desktop" aria-hidden="true"></i></button>
                    </td>
                </tr>
                {{end}}
                {{else}}
                <tr>
                    <td colspan="7" class="text-center">暂无记录</td>
                </tr>
                {{end}}
                </tbody>

            </table>
        </div>
        <div class="body-botton text-center">
            <span class="text" style="padding-right: 15px;" id="errorMessage"></span>

            <a href='{{urlfor "RelationController.Index" ":id" .Model.WebHookId}}' class="btn btn-success btn-sm">Back Server List</a>

        </div>
    </div>
</div>
<!-- Modal -->
<div class="modal fade" id="consoleModal" tabindex="-1" role="dialog" aria-labelledby="consoleModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="consoleModalLabel">Console Log</h4>
            </div>
            <div class="modal-body">
 <textarea name="shell" id="consoleContent" placeholder="" class="form-control" style="height: 150px;resize: none"></textarea>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
{{template "widgets/foot.html" .}}
<script type="text/javascript">
    $(function () {

        var consoleModalCache = $("#consoleModal").find(".modal-body").html();

       $(".btn-cancel-schedule").on("click",function () {
            var $this = $(this);
            var id = $this.attr("data-id");
            var url = '{{urlfor "SchedulerController.Cancel" ":scheduler_id" ""}}' + id;

            $.ajax({
                url : url,
                type :"POST",
                dataType : "json",
                success : function (res) {
                    if (res.errcode == 0){
                        $this.closest("tr").find(".label-default").removeClass("label-default").addClass("label-warning").text("执行取消");
                        $this.empty().remove()
                    }else{
                        alert(res.message);
                    }
                    $this.removeAttr("disabled");
                },
                error : function () {
                    $this.removeAttr("disabled");
                }

            });

            $this.attr("disabled","disabled");

       });

       $(".btn-schedule-console").on("click",function () {
           var id = $(this).attr("data-id");

            $("#consoleModal").attr("data-id",id).modal("show");
       });

       $("#consoleModal").on("shown.bs.modal",function () {
           CodeMirror.fromTextArea(document.getElementById("consoleContent"),{
               lineNumbers: true,
               mode: "text/x-sh",
               lineWrapping : true,
               matchBrackets: true,
               autoCloseBrackets: true,
               theme : "ambiance",
               readOnly : true
           });
       }).on("show.bs.modal",function (e) {
           $(this).find(".modal-body").html(consoleModalCache);

           var id = $(this).attr("data-id");

           var url = '{{urlfor "SchedulerController.Console" ":scheduler_id" ""}}' + id;
           $.ajax({
               url : url,
               dataType :"json",
               success : function (res) {
                   if(res.errcode === 0){
                       $("#consoleContent").val(res.data);
                   }else{
                       alert(res.message);
                   }
               },error : function () {
                   alert("服务器异常");
               }
           });
       });
       $(".btn-resume-schedule").on("click",function () {
            var $this = $(this);
            $this.attr("disabled","disabled");
           var id = $(this).attr("data-id");

           var url = '{{urlfor "SchedulerController.Resume" ":scheduler_id" ""}}' + id;

            $.ajax({
                url : url,
                dataType : "json",
                success : function (res) {
                    if(res.errcode === 0){
                        $this.closest("tr").find(".label").removeClass().addClass("label label-default").text("等待执行");
                        $this.empty().remove();
                    }
                },error : function () {
                    alert("服务器异常");
                },complete : function () {
                    $this.removeAttr("disabled");
                }
            })
       });
    });
</script>
</body>
</html>